package com.cgq.pro.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.cgq.base.action.BaseAction;
import com.cgq.pro.entity.Attachment;
import com.cgq.pro.service.AttachmentService;
import com.cgq.utils.CommonUtils;
/**
 * @author chenkaile
 */
@Controller
public class FileUploadAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private AttachmentService attachmentService;
	
	private List<File> file;
	private List<String> fileContentType;
	private List<String> fileFileName;
	private String ptypeid;
	private String id;
	/**业务类型*/
	private String businessType;
	
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getPtypeid() {
		return ptypeid;
	}
	public void setPtypeid(String ptypeid) {
		this.ptypeid = ptypeid;
	}
	public String getBusinessType() {
		return businessType;
	}
	public void setBusinessType(String businessType) {
		this.businessType = businessType;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * 上传文件
	 */
	public void upload(){
		try {
			String rootpath=getRootPath();
			File path =new File(rootpath+"upload/"+ptypeid+"/") ;
			if(!path.exists()){
				path.mkdirs();
			}
			for (int i = 0; i < file.size(); i++) {
				String fullFileName = path+"/" + fileFileName.get(i);
				String filePath="upload/"+ptypeid+"/"+fileFileName.get(i);
				FileUtils.copyFile(file.get(i), new File(fullFileName));
				Attachment entity=new Attachment();
				entity.setBusinessId(ptypeid);
				entity.setBusinessType(businessType);
				entity.setRefId(id);
				entity.setDateCre(new Date());
				entity.setExtName(CommonUtils.getFileExt(fileFileName.get(i)));
				entity.setFilePath(filePath);
				entity.setFileSize(file.get(i).length());
				entity.setName(CommonUtils.getFileName(fileFileName.get(i)));
				entity.setOriginalName(fileFileName.get(i));
				attachmentService.save(entity);
			}
		}catch (Exception e) {
			System.out.println("上传文件失败");
		}
	}
}
